package com.util;

import com.http.BaiduOAuthConfig;
import com.http.BaiduPanConfig;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class Folders {


    public static String saveFolder(String name, String desc, String fst, String scd, String thd, String ddl, String panId, String userId){
        Long createSeconds = System.currentTimeMillis() / 1000;
        String folderKey = RandomKey.getRandomKey(10);
        String sqlFormat = "insert into folder (create_seconds, folder_describe, " +
                "first_name, folder_key, folder_name, folder_open, " +
                "pan_id, second_name, show_file_name, stop_seconds, user_id) " +
                "values(%s, '%s', '%s', '%s', '%s', 1, %s, '%s', %s, %s, %s)";
        String sql = String.format(sqlFormat, createSeconds, desc, fst, folderKey, name, panId, scd, thd, ddl, userId);
        if(SQLHelp.update(sql) != 1){
            return "";
        }
        return folderKey;
    }

    public static void deleteFolder(String folder_key){
        String sql = String.format("delete from folder where folder_key = '%s'",folder_key);
        SQLHelp.update(sql);
    }

    public static void changeDeadline(String folder_key,String time){
        String sql = String.format("update folder set stop_seconds=%s where folder_key = '%s'",time,folder_key);
        SQLHelp.update(sql);
    }

    public static ArrayList<HashMap<String,String>> sortByCreateTime(String userId,String folderName){
        String sql;
        if (folderName.equals("")){
            sql = String.format("select* from folder where user_id = %s order by create_seconds asc",userId);
        }else{
            sql = String.format("select* from folder where user_id = %s and folder_name like '%%%s%%' order by create_seconds asc",userId,folderName);
        }
        return SQLHelp.selectQuery(sql);
    }

    public static ArrayList<HashMap<String,String>> sortByStopTime(String userId,String folderName){
        String sql;
        if (folderName.equals("")){
            sql = String.format("select* from folder where user_id = %s order by stop_seconds asc",userId);
        }else{
            sql = String.format("select* from folder where user_id = %s and folder_name like '%%%s%%' order by stop_seconds asc",userId,folderName);
        }
        return SQLHelp.selectQuery(sql);
    }

    public static ArrayList<HashMap<String,String>> isStar(String userId,String folderName){
        String sql;
        if (folderName.equals("")){
            sql = String.format("select* from folder where user_id = %s and star = 1", userId);
        }else {
            sql = String.format("select* from folder where user_id = %s and star = 1 and folder_name like '%%%s%%'", userId, folderName);
        }
        return SQLHelp.selectQuery(sql);
    }


    public static String getFolderFileName(String folderKey, String fst, String scd, String fileName){
        String sqlFormat = "select * from folder where folder_key = '%s';";
        String sql = String.format(sqlFormat, folderKey);
        ArrayList<HashMap<String, String>> res = SQLHelp.selectQuery(sql);
        if(res.isEmpty()){
            return fileName;
        }
        HashMap<String, String> hashMap = res.get(0);
        StringBuilder stringBuilder = new StringBuilder();
        if(!hashMap.get("first_name").equals("")){
            stringBuilder.append(fst);
            stringBuilder.append("_");
        }
        if(!hashMap.get("second_name").equals("")){
            stringBuilder.append(scd);
            stringBuilder.append("_");
        }
        int i = fileName.lastIndexOf(".");
        String sub;
        if(i == -1){
            sub = "";
        }else{
            sub = fileName.substring(i);
            fileName = fileName.substring(0, i);
        }
        if(hashMap.get("show_file_name").equals("1")){
            stringBuilder.append(fileName);
            stringBuilder.append("_");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        stringBuilder.append(sub);
        return stringBuilder.toString();

    }

    public static boolean saveFolderFile(String filePath, String folderKey) throws IOException {
        String folderFormat = "select * from folder where folder_key = '%s'";
        String panFormat = "select * from baiduoauthtable where pan_id = %s";
        String sql = String.format(folderFormat, folderKey);
        ArrayList<HashMap<String, String>> res = SQLHelp.selectQuery(sql);
        if(res.isEmpty()){
            return false;
        }
        String pan_id = res.get(0).get("pan_id");
        if(pan_id.equals("0")){
            return false;
        }
        String accessToken = BaiduOAuthConfig.checkAndGetPanAccessToken(Integer.parseInt(pan_id));
        if(accessToken.equals("")){
            return false;
        }
        // 创建文件夹
        if(!BaiduPanConfig.createAppFolder(accessToken)){
            return false;
        }
        if(!BaiduPanConfig.createFolder(accessToken, folderKey)){
            return false;
        }
        // 这里已经创建成功了

        // https://blog.csdn.net/Baldprogrammer/article/details/116892593
        // 创建文件
        if(!BaiduPanConfig.uploadFile(accessToken, folderKey, filePath)){
            return false;
        }
        return true;
    }

}
